package com.dyrj.question.controller;

import com.dyrj.question.dao.PDayanswerMapper;
import com.dyrj.question.dao.PDayawardMapper;
import com.dyrj.utils.DateUtil;
import com.dyrj.utils.ExcelExportHelper;
import com.dyrj.utils.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.activation.FileTypeMap;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.dyrj.question.service.IPDayanswerService;
import com.dyrj.question.bean.PDayanswer;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 日答题管理 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Controller
@RequestMapping("/question/p-dayanswer")
public class PDayanswerController {

    @Resource
    private IPDayanswerService service;
    @Resource
    private PDayanswerMapper pDayanswerMapper;
    @GetMapping("/index")
    public String index(ModelMap map) {
    return "question/index_p-dayanswer";
    }

    @GetMapping("/pointrank")
    public String pointrank(ModelMap map) {
        return "question/report_pointrank";
    }
    @GetMapping("/dtcjhz")
    public String dtcjhz(ModelMap map) {
        return "question/report_dtcjhz";
    }
//    智慧币持有情况汇总
    @GetMapping("/zhbcyqkhz")
    public String zhbcyqkhz(ModelMap map) {
        return "question/report_zhbcyqkhz";
    }
    /**
    * @Title: table
    * @Description: (表格数据) 列表
    * @return PageInfo<PDayanswer>    返回类型
    */
    @GetMapping("/table")
    @ResponseBody
    public PageResultMsg table(PDayanswer record) {
        return service.table(record);
    }
    @GetMapping("/table1")
    @ResponseBody
    public PageResultMsg table1(PDayanswer record) {
        return service.table1(record);
    }
    @GetMapping("/tableHj")
    @ResponseBody
    public PageResultMsg tableHj(PDayanswer record) {
        return service.tableHj(record);
    }
    @GetMapping("/table2")
    @ResponseBody
    public PageResultMsg table2(PDayanswer record) {
        return service.table2(record);
    }
    @GetMapping("/table3")
    @ResponseBody
    public PageResultMsg table3(PDayanswer record) {
        return service.table3(record);
    }
    /**
    * @Title: add
    * @Description: (新增)
    * @param record
    * @return ResultMsg    返回类型
    */
    @PostMapping("/")
    @ResponseBody
    public ResultMsg add(PDayanswer record) {
        return service.add(record);
    }

    /**
    * @Title: info
    * @Description: (详情)
    * @param id
    * @return PDayanswer    返回类型
    */
    @GetMapping("/{id}")
    @ResponseBody
    public PDayanswer info(@PathVariable String id) {
        return service.info(id);
    }

    /**
    * @Title: update
    * @Description: (更新)
    * @param record
    * @return ResultMsg    返回类型
    */
    @PutMapping("/")
    @ResponseBody
    public ResultMsg update(PDayanswer record) {
        return service.update(record);
    }
    /**
    * @Title: delete
    * @Description: (删除)
    * @param id
    * @return ResultMsg    返回类型
    */
    @DeleteMapping("/{id}")
    @ResponseBody
    public ResultMsg delete(@PathVariable String id) {
        return service.delete(id);
    }
    @GetMapping("/export")
    public  void export(@RequestParam(required = false) Map map, HttpServletResponse resp) throws Exception {
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        String heads[] = {"答题时间","答题次数","答题人姓名","部门名称","分数","积分","备注"};
        String [] properties={"dttime1","dtcn","usrname","deptname","score","point","bz"};
        String ksny = (String)map.get("ksny");
        String jsny = (String)map.get("jsny");
        PDayanswer pDayanswer = new PDayanswer();
        pDayanswer.setKsny(ksny);
        pDayanswer.setJsny(jsny);
        List list = pDayanswerMapper.selectList(pDayanswer);
        HSSFWorkbook hssfWorkbook = excelExportHelper.exportExcel(heads,properties,list,"每日答题情况统计");
        String filename = DateUtil.getAllTime()+"每日答题情况统计.xls";
        try {
            resp.setHeader("Content-Disposition", "attachment;filename="+filename);
            resp.setContentType(FileTypeMap.getDefaultFileTypeMap()
                    .getContentType(filename)+ "; charset=UTF-8");
            resp.setHeader("Content-disposition", "attachment; filename="
                    + new String(filename.getBytes("UTF-8"),
                    "ISO8859_1"));
            resp.setHeader("Cache-Control", "no-cache");
            resp.setDateHeader("Expires", 0);
            ServletOutputStream out =resp.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(out);
            bufferedOutPut.flush();
            hssfWorkbook.write(bufferedOutPut);
            bufferedOutPut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (hssfWorkbook != null) {
                try {
                    hssfWorkbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @GetMapping("/export1")
    public  void export1(@RequestParam(required = false) Map map, HttpServletResponse resp) throws Exception {
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        String heads[] = {"日期","总答题人数","总答题次数","智慧币出现次数","智慧币发放总金额"};
        String [] properties={"rq","percn","cn","awardcn","award"};
        String ny = (String)map.get("ny");
        PDayanswer pDayanswer = new PDayanswer();
        pDayanswer.setNy(ny);
        List list = pDayanswerMapper.getDttjList1(pDayanswer);
        HSSFWorkbook hssfWorkbook = excelExportHelper.exportExcel(heads,properties,list,"答题抽奖情况汇总");
        String filename = DateUtil.getAllTime()+"答题抽奖情况汇总.xls";
        try {
            resp.setHeader("Content-Disposition", "attachment;filename="+filename);
            resp.setContentType(FileTypeMap.getDefaultFileTypeMap()
                    .getContentType(filename)+ "; charset=UTF-8");
            resp.setHeader("Content-disposition", "attachment; filename="
                    + new String(filename.getBytes("UTF-8"),
                    "ISO8859_1"));
            resp.setHeader("Cache-Control", "no-cache");
            resp.setDateHeader("Expires", 0);
            ServletOutputStream out =resp.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(out);
            bufferedOutPut.flush();
            hssfWorkbook.write(bufferedOutPut);
            bufferedOutPut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (hssfWorkbook != null) {
                try {
                    hssfWorkbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @GetMapping("/export2")
    public  void export2(@RequestParam(required = false) Map map, HttpServletResponse resp) throws Exception {
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        String heads[] = {"姓名","三级单位","持有智慧币金额","已用使用智慧币金额","领取智慧币总金额"};
        String [] properties={"NAME","dept3","syje","shopprice","award"};
        String dept3 = (String)map.get("dept3");
        PDayanswer pDayanswer = new PDayanswer();
        pDayanswer.setDept3(dept3);
        List list = pDayanswerMapper.getDttjList2(pDayanswer);
        HSSFWorkbook hssfWorkbook = excelExportHelper.exportExcel(heads,properties,list,"智慧币持有情况汇总");
        String filename = DateUtil.getAllTime()+"智慧币持有情况汇总.xls";
        try {
            resp.setHeader("Content-Disposition", "attachment;filename="+filename);
            resp.setContentType(FileTypeMap.getDefaultFileTypeMap()
                    .getContentType(filename)+ "; charset=UTF-8");
            resp.setHeader("Content-disposition", "attachment; filename="
                    + new String(filename.getBytes("UTF-8"),
                    "ISO8859_1"));
            resp.setHeader("Cache-Control", "no-cache");
            resp.setDateHeader("Expires", 0);
            ServletOutputStream out =resp.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(out);
            bufferedOutPut.flush();
            hssfWorkbook.write(bufferedOutPut);
            bufferedOutPut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (hssfWorkbook != null) {
                try {
                    hssfWorkbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @GetMapping("/export3")
    public  void export3(@RequestParam(required = false) Map map, HttpServletResponse resp) throws Exception {
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        String heads[] = {"排名","三级单位","四级单位","姓名","积分","用时","备注"};
        String [] properties={"rankpx","dept3","dept4","usrname","point","usetime","bz"};
        String ksny = (String)map.get("ksny");
        String jsny = (String)map.get("jsny");
        PDayanswer pDayanswer = new PDayanswer();
        pDayanswer.setKsny(ksny);
        pDayanswer.setJsny(jsny);
        List list = pDayanswerMapper.getPersonRankHjList(pDayanswer);
        HSSFWorkbook hssfWorkbook = excelExportHelper.exportExcel(heads,properties,list,"积分排名情况统计");
        String filename = DateUtil.getAllTime()+"积分排名情况统计.xls";
        try {
            resp.setHeader("Content-Disposition", "attachment;filename="+filename);
            resp.setContentType(FileTypeMap.getDefaultFileTypeMap()
                    .getContentType(filename)+ "; charset=UTF-8");
            resp.setHeader("Content-disposition", "attachment; filename="
                    + new String(filename.getBytes("UTF-8"),
                    "ISO8859_1"));
            resp.setHeader("Cache-Control", "no-cache");
            resp.setDateHeader("Expires", 0);
            ServletOutputStream out =resp.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(out);
            bufferedOutPut.flush();
            hssfWorkbook.write(bufferedOutPut);
            bufferedOutPut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (hssfWorkbook != null) {
                try {
                    hssfWorkbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
